﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports Utilerias
Imports WSFactory
Imports MySql.Data.MySqlClient

Public Class ImpFederalesDAL
    Private cadConex As String = ""

    Public Sub New(ByVal pCadConex As String)
        Me.cadConex = pCadConex
    End Sub

#Region "FuncionesUtilizables"

    Public Function actualizaImpFederales(ByVal pImpFederales As ImpFederalesBE) As Boolean
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "UPDATE impfederales " & _
                                         "SET IVATrasTasa = @IVATrasTasa, ActIVACero = @ActIVACero, ActIVAExento = @ActIVAExento, " &
                                                "IVARetAplica = @IVARetAplica, IVARetTasa = @IVARetTasa, " &
                                                "IEPSAplica = @IEPSAplica, IEPSTasa = @IEPSTasa, " &
                                                "ISRAplica = @ISRAplica, ISRTasa = @ISRTasa " & _
                                         "; "
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Clear()
                    myComm.Parameters.AddWithValue("@IVATrasTasa", pImpFederales.IVATrasladadoTasa)
                    myComm.Parameters.AddWithValue("@ActIVACero", Conversiones.convertBoolToSN(pImpFederales.ActivarIVATasaCero))
                    myComm.Parameters.AddWithValue("@ActIVAExento", Conversiones.convertBoolToSN(pImpFederales.ActivarIVAExento))
                    myComm.Parameters.AddWithValue("@IVARetAplica", Conversiones.convertBoolToSN(pImpFederales.AplicarIVARetenido))
                    myComm.Parameters.AddWithValue("@IVARetTasa", pImpFederales.IVARetenidoTasa)
                    myComm.Parameters.AddWithValue("@IEPSAplica", Conversiones.convertBoolToSN(pImpFederales.AplicarIEPS))
                    myComm.Parameters.AddWithValue("@IEPSTasa", pImpFederales.IEPSTasa)
                    myComm.Parameters.AddWithValue("@ISRAplica", Conversiones.convertBoolToSN(pImpFederales.AplicarISR))
                    myComm.Parameters.AddWithValue("@ISRTasa", pImpFederales.ISRTasa)
                    myConn.Open()
                    If (myComm.ExecuteNonQuery() >= 1) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function getImpFederales() As ImpFederalesBE
        Dim tmpImpFederales As ImpFederalesBE
        Try
            Using myConn As MySqlConnection = New MySqlConnection(Me.cadConex)
                Using myComm As MySqlCommand = myConn.CreateCommand()
                    myComm.CommandText = "SELECT IdImpuestos, IVATrasTasa, ActIVACero, ActIVAExento, IVARetAplica, IVARetTasa, " & _
                                                "IEPSAplica, IEPSTasa, ISRAplica, ISRTasa " & _
                                         "FROM impfederales " & _
                                         "ORDER BY IdImpuestos ASC " &
                                         "LIMIT 1 ;"
                    myComm.CommandType = CommandType.Text
                    myConn.Open()
                    Using myDR As MySqlDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpImpFederales = ImpFederalesFact.Build(myDR)
                        Else
                            tmpImpFederales = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpImpFederales

        Catch ex As Exception
            Throw
        Finally
            tmpImpFederales = Nothing
        End Try
    End Function

#End Region
End Class
